package com.hopu.v1.controller.admin.classes.attendance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.classes.Student;
import com.hopu.v1.pojo.classes.attendance.Attendance;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.service.admin.classes.StudentService;
import com.hopu.v1.service.admin.classes.attendance.AttendanceService;
import com.hopu.v1.vo.AttendanceWithVerifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;

@CrossOrigin
@RestController
@RequestMapping("/api/v1/admin/classes/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    @Autowired
    private StudentService studentService;

    @PostMapping("insert")
    public void insert(@RequestBody AttendanceWithVerifyVo attendanceWithVerifyVo){
        Integer studentId = attendanceWithVerifyVo.getStudentId();
        //根据传来的学生id查询学生班级
        Student student = studentService.getById(studentId);
        Integer studentId1 = Integer.valueOf(student.getStudentId());
        Integer classId = Integer.valueOf(student.getClassId());

        //请假开始日期
        LocalDateTime startTime = attendanceWithVerifyVo.getStartTime();
        LocalDate startTime1 = startTime.toLocalDate();
        //请假截止日期
        LocalDateTime endTime = attendanceWithVerifyVo.getEndTime();
        LocalDate endTime1 = endTime.toLocalDate();


        //请假天数
        long days = startTime1.until(endTime1, ChronoUnit.DAYS);
        ArrayList<Attendance> attendances = new ArrayList<>();

        //循环执行插入
        for (int i = 0; i <= days; i++) {
            Attendance attendance = new Attendance();
            //设置班级id
            attendance.setClassId(classId);
            //设置学生id
            attendance.setStudentId(studentId1);
            //设置状态为3：请假
            attendance.setAttendanceStatus(3);

            LocalDate localDate = startTime1.plusDays(i);
            attendance.setAttendanceDate(localDate);
            System.out.println("attendance = " + attendance);
            attendances.add(attendance);
        }
        System.out.println("attendances = " + attendances);
        //批量插入
        attendanceService.saveBatch(attendances);
    }


    @GetMapping("selectAll")

    public R selectAll(Page<Attendance> page,Attendance attendance){

        return R.ok(this.attendanceService.page(page,new QueryWrapper<>(attendance)));
    }

    /**
     *
     * @return
     */
    @GetMapping("getAllByStartHours")
    public R getAllByStartHours(){
        return R.ok(this.attendanceService.getAllByStartHours());
    }

    @GetMapping("getAllByEndHours")
    public R getAllByEndHours(){
        return R.ok(this.attendanceService.getAllByEndHours());
    }

    @PutMapping("updateStatusById")
    public R updateStatusById(@PathVariable Long id){return R.ok(this.attendanceService.updateStatusById(id));}

    @GetMapping("getStatusById")
    public R getStatusById(@RequestParam Long id){
        return R.ok(this.attendanceService.getStatusById(id));
    }
}
